#!/bin/sh
# $Id: autogen_docs_html.sh,v 1.3 2004/09/22 00:30:45 jimb Exp $
# Author: Jim E. Brooks http://www.jimbrooks.org
# Auto-generate HTML documentation using doxygen.
# Prereq: Must be run from top-level dir.
# Args  : $1 = output directory (where to write HTML files)
#-------------------------------------------------------------------------------

#============= BEGIN CONFIGURATION =============================================

# Source dirs.  Separate by spaces.
C_DIRS="src"
H_DIRS="src/h"

# Source file extensions.  C_EXT can be used for C++.
C_EXT=.c
H_EXT=.h

# Any file used to just check if CWD is top-level dir of project.
TOP_LEVEL_FILE=Hypersrc.pl

# Sed script.
SED_SCRIPT=autogen_docs_html.sed

#============= END CONFIGURATION ===============================================

# Assign vars with args.
if [ "$1" = "" ]; then
	echo "ERROR: Args required"
    exit 1
fi
DOC_DIR=$1

# Ensure doxygen is installed.
if ( ! which doxygen >/dev/null 2>/dev/null ); then
    echo "ERROR: doxygen is required"
    exit 1
fi

# Ensure this script is run from the top-level dir.
[ -f $TOP_LEVEL_FILE ] || exit 1

# Make output directory.
mkdir -p $DOC_DIR
cd $DOC_DIR || exit 1

# Start with clean dir.
rm -f  *${C_EXT}  *${H_EXT}
rm -rf html

# Pass every src file thru a sed script which transforms my style
# for C function comments into what doxygen expects as so:
#
# /*****************************************************************************
#  * 
#  *****************************************************************************/
#
# /** <-- two asterisks is what doxygen looks for
#  * 
#  *****************************************************************************/
#
DoFile() {
    FILE=$1
    BASE_FILE=${FILE##*/}
    cat $FILE | sed -f ../${SED_SCRIPT} > ${BASE_FILE}
}
for DIR in $C_DIRS;
do
    for FILE in ../${DIR}/*${C_EXT}; do DoFile $FILE; done
done
for DIR in $H_DIRS;
do
    for FILE in ../${DIR}/*${H_EXT}; do DoFile $FILE; done
done

# Run doxygen.
rm -f doxygen.config
doxygen -g doxygen.config || exit 1
echo "GENERATE_LATEX  = no " >> doxygen.config
echo "EXTRACT_ALL     = yes" >> doxygen.config
echo "EXTRACT_PRIVATE = yes" >> doxygen.config
echo "EXTRACT_STATIC  = yes" >> doxygen.config
doxygen doxygen.config || exit 1

# Cleanup.
rm -f doxygen.config
rm -f  *${C_EXT} *${H_EXT}

exit 0
